package com.hui.service;

import com.hui.dao.ManagerDao;
import com.hui.entity.Manager;
import com.hui.vo.ManagerVo;

import java.util.List;


/**
 * @Description 管理员业务层
 */
public class ManagerService {

    public Manager login(String loginName, String loginPwd) {
        ManagerDao dao = new ManagerDao();

        //RSA加密算法
//        String newPwd = Encrypt.encrypt(loginName, loginPwd);

        Manager manager = dao.findByUserNameAndPassword(loginName, loginPwd);
        return manager;
    }

    public ManagerVo findByMgrId(int mgrId) {
        ManagerDao dao = new ManagerDao();
        return dao.findById(mgrId);
    }

    public void updateById(Manager mgr) {
        ManagerDao dao = new ManagerDao();
        dao.updateManager(mgr);
    }

    public List<ManagerVo> findManagerAll() {
        ManagerDao dao = new ManagerDao();
        return dao.findManagerAll();
    }

    public void delManagerById(int mgrId) {
        ManagerDao dao = new ManagerDao();
        dao.delManagerById(mgrId);
    }

    public void addManager(Manager manager, String[] roleIds) {
        ManagerDao dao = new ManagerDao();
        int mgrId = dao.addManager(manager);

        for (String menuId : roleIds) {
            dao.addManagerRole(mgrId, Integer.parseInt(menuId));
        }

    }

    public void updateManger(Manager manager, String[] roleIds) {
        ManagerDao dao = new ManagerDao();
        //管理员信息修改方法
        dao.updateManager(manager);

        //根据管理员id删除和职位的关联管理
        dao.deleteManagerRoleByMgrId(manager.getMgrId());

        //重新添加管理员和职位的关系
        for (String roleId : roleIds) {
            dao.addManagerRole(manager.getMgrId(), Integer.parseInt(roleId));
        }
    }


    public void deleteManagerRoleByRoleId(int mgrId, int roleId) {
        ManagerDao dao = new ManagerDao();
        dao.deleteManagerRoleByRoleId(mgrId, roleId);
    }


}
